//
//  MyOrderViewController.m
//  囧途
//
//  Created by fly on 16/3/3.
//  Copyright © 2016年 fly. All rights reserved.
//

#import "MyOrderViewController.h"
#import "MyorderTableViewCell.h"
#import <AVUser.h>
#import <AVObject.h>
#import <AVQuery.h>
#import <AVRelation.h>
@interface MyOrderViewController ()<UITableViewDataSource,UITableViewDelegate>
{

}
@property(nonatomic, strong)UITableView * tableView;

@property(nonatomic, strong) AVUser * currentUser;

@property(nonatomic, strong)AVObject * obj;

@property(nonatomic, strong)NSMutableDictionary * dictionary;

@property(nonatomic, strong)NSDictionary * dic;

@property(nonatomic, strong)NSMutableArray * resultArray;

@property(nonatomic, strong) NSMutableArray * endArray;

@property(nonatomic, strong)NSMutableArray * startArray;

@end

@implementation MyOrderViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _currentUser = [AVUser currentUser];
    
    [self.view addSubview:self.tableView];
    
    [self initializeDateSoure];
    
}

-(void)initializeDateSoure
{
    AVObject *studentTom = [AVObject objectWithoutDataWithClassName:@"_User" objectId:_currentUser.objectId];
    
    // 读取 AVRelation 对象
    AVRelation *relation = [studentTom relationForKey:@"Trip"];
    
    // 获取关系查询
    AVQuery *query = [relation query];
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *courses, NSError *error) {
        
        
        _dictionary = [NSMutableDictionary dictionary];
        
        _resultArray = [NSMutableArray array];
        
        _endArray = [NSMutableArray array];
        
        _startArray = [NSMutableArray array];
        
        for (int i=0; i<courses.count; i++) {
            
            [_resultArray addObject:courses[i][@"localData"] ];
            
            _dictionary = _resultArray[i];
            
            [_endArray addObject:_dictionary[@"end"]];
            
            [_startArray addObject:_dictionary[@"start"]];
        }
        
        NSLog(@"===============================================%@", _resultArray);
        
        [self.tableView reloadData];
        
    }];
    
}

#pragma mark-delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return _resultArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identfy = @"Cell";
    
    MyorderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identfy];
    
    if (!cell) {
        cell = [[MyorderTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identfy];
        
        if (_resultArray !=nil) {
            
            cell.location.text = _startArray[indexPath.row];
            
            cell.aim.text = _endArray[indexPath.row];
            
        }
        
       
}
    return cell;


}




#pragma mark- getter

-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        
        _tableView.delegate = self;
        
        _tableView.dataSource =self;
        
        _tableView.rowHeight = 140;
    }

    return _tableView;
}



@end
